<template>
  <el-dialog 
    :title="dialogTitle" 
    :visible.sync="dialogVisible" 
    width="600px" 
    append-to-body 
    @close="cancel"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="拒绝理由" prop="auditOpinion">
        <el-input
          v-model="form.auditOpinion"
          type="textarea"
          :rows="6"
          placeholder="请输入拒绝理由（必填）"
          maxlength="500"
          show-word-limit
        />
      </el-form-item>
      <el-alert
        v-if="isModificationReject"
        title="注意：拒绝修改申请后，题目将恢复为「已通过」状态，原内容保持不变"
        type="info"
        :closable="false"
        style="margin-top: 10px;"
      />
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { rejectTopic } from "@/api/system/topic";

export default {
  name: "RejectDialog",
  data() {
    return {
      // 是否显示弹出层
      dialogVisible: false,
      // 表单参数
      form: {
        topicId: null,
        auditOpinion: null
      },
      // 是否是修改申请的拒绝
      isModificationReject: false,
      // 表单校验
      rules: {
        auditOpinion: [
          { required: true, message: "拒绝理由不能为空", trigger: "blur" },
          { min: 5, message: "拒绝理由至少5个字符", trigger: "blur" }
        ]
      }
    };
  },
  computed: {
    dialogTitle() {
      return this.isModificationReject ? '拒绝修改申请' : '拒绝审批';
    }
  },
  methods: {
    /** 打开对话框 */
    openDialog(topicId, isModification = false) {
      this.reset();
      this.form.topicId = topicId;
      this.isModificationReject = isModification;
      this.dialogVisible = true;
    },
    // 表单重置
    reset() {
      this.form = {
        topicId: null,
        auditOpinion: null
      };
      this.isModificationReject = false;
      this.resetForm("form");
    },
    /** 取消按钮 */
    cancel() {
      this.dialogVisible = false;
      this.reset();
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          rejectTopic(this.form.topicId, this.form.auditOpinion).then(response => {
            this.$modal.msgSuccess("拒绝成功");
            this.dialogVisible = false;
            this.$emit("success");
          });
        }
      });
    }
  }
};
</script>